![]() | |
|
|
|
To access the contents, click the chapter and section titles.
Bug Proofing Visual Basic: A Guide to Error Handling and Prevention
To set DebugMode, the program uses a trick involving the compilers behavior. If the program is compiled, it contains no Debug statements; in particular, it contains no Debug.Print statements. To determine whether is has been compiled or not, the program tries to execute the command Debug.Print 1 / 0. If the program has been compiled, this statement has been removed and the program continues. On the other hand, if the program is running in the development environment, it tries to display the value 1 / 0. This generates a divide by 0 error. The program uses an On Error statement to trap this error and set DebugMode to True. The second technique the program uses is to move the argument verifying code into a subroutine. Subroutine VerifyVariantType checks that a variant argument is of the correct data type. This subroutine reduces the amount of duplicated code and shortens argument checking.
True if we are in the development environment.
Public DebugMode As Boolean
See if we are running in the development environment
or a compiled program. Run this routine at startup.
Public Sub SetDebugMode()
On Error GoTo CompiledVersion
If the program is compiled, the following
Debug statement has been removed so it will
not generate an error.
Debug.Print 1 / 0
DebugMode = False
Exit Sub
CompiledVersion:
We got an error so the Debug statement must
be working.
DebugMode = True
End Sub
Verify that the variant is of the correct type.
Public Sub VerifyVariantType(param As Variant, _
type_name As String, routine_name As String)
If Not (TypeName(param) = type_name) Then
If DebugMode Then
Stop
Else
Err.Raise err_INVALID_PARAMETER, _
MyProgram. & routine_name, _
Parameter should be of type & _
type_name & _
but it is of type & _
TypeName(param) & _
with value & param
End If
End If
End Sub
The program calls subroutine SetDebugMode when it starts. After that it can use VerifyVariantType to validate variant parameter types. The following code shows how a subroutine can verify that its parameters are strings.
Set the DebugMode variable when we start.
SetDebugMode
:
Make sure the parameters are strings.
VerifyVariantType param1, String, MySubroutine
VerifyVariantType param1, String, MySubroutine
VerifyVariantType param1, String, MySubroutine
This version automatically determines whether it is running in the development environment or in a compiled executable. The code is more readable and the VerifyVariantType subroutine provides a good place to add error-logging code later. This method has the disadvantages that it is slightly slower and uses slightly more memory than the previous version. In the previous version, when the conditional compilation symbol DEBUG_MODE is False, the compiler removes the error checking code from the executable version. That reduces the amount of code in the executable. It also means the program does not need to execute an If statement to determine whether or not it is running compiled. Validate Optional ParametersIn addition to normal parameters, a routine should verify that any optional parameters it receives make sense. For example, if a subroutine takes pairs of numbers as parameters, it should verify that the calling routine has passed in an even number of values. Use the IsMissing function to determine which parameters are present. Because optional parameters in VB4 are always variants, you should also verify that they have the correct data types and that they have reasonable values. Program Circles4 draws concentric circles as shown in Figure 5.1. This program uses the DrawCircles subroutine shown in the following code to draw its circles. The routine takes as parameters the center of the circles and the radius of the largest circle. Optional color and aspect parameters let the program specify a color and aspect ratio for the circles. The routine verifies that the color parameter is numeric and that its value is nonnegative.
|
|
Products | Contact Us | About Us | Privacy | Ad Info | Home
Use of this site is subject to certain Terms & Conditions, Copyright © 1996-1999 EarthWeb Inc. All rights reserved. Reproduction whole or in part in any form or medium without express written permision of EarthWeb is prohibited.
|